package qeb.handheldmedical.exam;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import qeb.handheldmedical.R;
import qeb.handheldmedical.adapter.LineExamAdapter;
import qeb.handheldmedical.adapter.SlideListView;
import qeb.handheldmedical.dialog.MultiDialog;
import qeb.handheldmedical.helper.WSHelper;
import qeb.handheldmedical.model.User;
import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

/**
 * @author 作者(dugaoyi) E-mail:dugaoyi@163.com
 * @version 创建时间：2016年9月1日 下午4:15:52
 * @declaration 开放关闭试卷
 */
public class ExamOpenClose extends Activity {
	Button btn_title_back, btn_title_right;
	private TextView tv_top_title;
	User usr = new User();
	private String examType;
	private JSONArray examJsonArray;
	private SlideListView slvexamlist;
	private LineExamAdapter adapter;
	private MultiDialog mDialog;
	private String examID, examTitle, examstate = "";

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onCreate(android.os.Bundle)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.exam_open_close);
		usr = (User) getIntent().getSerializableExtra("userinfo");
		examType = (String) getIntent().getSerializableExtra("examType");

		tv_top_title = (TextView) findViewById(R.id.tv_top_title);
		tv_top_title.setText("开放面授考试试卷");
		btn_title_back = (Button) findViewById(R.id.btn_title_back);
		btn_title_back.setOnClickListener(MyOnClickListener);

		btn_title_right = (Button) findViewById(R.id.btn_title_right);
		btn_title_right.setOnClickListener(MyOnClickListener);

		slvexamlist = (SlideListView) findViewById(R.id.slvexamlist);

		getData();
		bindList();
	}

	/**
	 * 绑定列表
	 */
	private void bindList() {
		// TODO Auto-generated method stub
		if (examJsonArray != null && examJsonArray.length() > 0) {

			if (adapter != null) {
				adapter.notifyDataSetChanged();
			}

			adapter = new LineExamAdapter(examJsonArray.length(),
					ExamOpenClose.this, examJsonArray, true);

			slvexamlist.setAdapter(adapter);

			slvexamlist.setOnItemClickListener(new OnItemClickListener() {

				public void onItemClick(AdapterView<?> parent, View view,
						int position, long id) {

					if (!usr.JBMC.equals("0")) {
						mDialog = new MultiDialog(ExamOpenClose.this,
								MultiDialog.DialogType.ALERT);
						mDialog.setMessage("您无权限设置试卷！");
						mDialog.show();
						return;
					}

					examID = "";
					examTitle = "";
					examstate = "";
					JSONObject xt;
					try {
						xt = examJsonArray.getJSONObject(position);
						examID = xt.getString("EXAMID");
						examTitle = xt.getString("TITLE");
						examstate = xt.getString("EXAMSTATE");// 只有面授考试才要判断开放
						mDialog = new MultiDialog(ExamOpenClose.this,
								MultiDialog.DialogType.CONFIRM);
						String strMessage = "";// 提示信息
						if (examstate.equals("0")) {
							strMessage = "您是否要关闭【" + examTitle + "】试卷！";
						} else {
							strMessage = "您是否要开放【" + examTitle + "】试卷！";
						}
						mDialog.setMessage(strMessage);
						mDialog.setDialogGoListener(new OnClickListener() {

							@Override
							public void onClick(View v) {
								// TODO Auto-generated method stub
								mDialog.cancel();
								Map<String, String> map = new HashMap<String, String>();
								map.put("examid", examID);
								map.put("examstate", examstate);
								JSONArray updateJson = WSHelper
										.getWebserviceFun("SetExamState", map);
								try {
									JSONObject tep = updateJson
											.getJSONObject(0);

									Toast tt = Toast.makeText(
											ExamOpenClose.this,
											tep.getString("pda_error_message"),
											Toast.LENGTH_LONG);
									tt.setGravity(Gravity.CENTER, 0, 0);
									tt.show();
									getData();
									bindList();

								} catch (JSONException e) {
									// TODO Auto-generated catch block
									e.printStackTrace();
								}

							}
						});
						mDialog.show();
					} catch (Exception ex) {
					}
				}

			});
		}
	}

	/**
	 * 读取数据
	 */
	private void getData() {
		// TODO Auto-generated method stub
		if (usr.BQID != null) {
			Map<String, String> map = new HashMap<String, String>();
			map.put("studyrange", "0");
			map.put("starttime", "");
			map.put("endtime", "");
			map.put("examtype", examType);
			map.put("userid", usr.GH);
			examJsonArray = WSHelper.getWebserviceFun("GetStudyList", map);
		}
	}

	private OnClickListener MyOnClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			switch (v.getId()) {
			case R.id.btn_title_back:
				ExamOpenClose.this.finish();
				break;

			default:
				break;
			}
		}

	};
}
